﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using RBS.Common;
using RBS.Core.Model;
namespace RBS.Core.BLL
{
    //数据字典
    public partial class SYS_DictInfo
    {

        private readonly RBS.Core.DAL.SYS_DictInfo dal = new RBS.Core.DAL.SYS_DictInfo();
        public SYS_DictInfo()
        { }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DictID)
        {
            return dal.Exists(DictID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.SYS_DictInfo model)
        {
          return  dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.SYS_DictInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DictID)
        {

            return dal.Delete(DictID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.SYS_DictInfo GetModel(int DictID)
        {

            return dal.GetModel(DictID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public RBS.Core.Model.SYS_DictInfo GetModelByCache(int DictID)
        {

            string CacheKey = "SYS_DictInfoModel-" + DictID;
            object objModel = RBS.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(DictID);
                    if (objModel != null)
                    {
                        int ModelCache = RBS.Common.ConfigHelper.GetConfigInt("ModelCache");
                        RBS.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (RBS.Core.Model.SYS_DictInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.SYS_DictInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }


       

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.SYS_DictInfo> DataTableToList(DataTable dt)
        {
            List<RBS.Core.Model.SYS_DictInfo> modelList = new List<RBS.Core.Model.SYS_DictInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                RBS.Core.Model.SYS_DictInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new RBS.Core.Model.SYS_DictInfo();
                    model.DictID = int.Parse(dt.Rows[n]["DictID"].ToString());
                    model.LastUpdateUserName = dt.Rows[n]["LastUpdateUserName"].ToString();
                    model.LastUpdateUserFullName = dt.Rows[n]["LastUpdateUserFullName"].ToString();
                    if (dt.Rows[n]["LastUpdateTime"].ToString() != "")
                    {
                        model.LastUpdateTime = DateTime.Parse(dt.Rows[n]["LastUpdateTime"].ToString());
                    }
                    model.CreateUserName = dt.Rows[n]["CreateUserName"].ToString();
                    model.CreateUserFullName = dt.Rows[n]["CreateUserFullName"].ToString();
                    if (dt.Rows[n]["CreateDateTime"].ToString() != "")
                    {
                        model.CreateDateTime = DateTime.Parse(dt.Rows[n]["CreateDateTime"].ToString());
                    }
                    model.DictName = dt.Rows[n]["DictName"].ToString();
                    model.DictCode = dt.Rows[n]["DictCode"].ToString();
                    if (dt.Rows[n]["DictType"].ToString() != "")
                    {
                        model.DictType = int.Parse(dt.Rows[n]["DictType"].ToString());
                    }
                    if (dt.Rows[n]["ParentID"].ToString() != "")
                    {
                        model.ParentID = int.Parse(dt.Rows[n]["ParentID"].ToString());
                    }
                    model.Path = dt.Rows[n]["Path"].ToString();
                    if (dt.Rows[n]["Sequence"].ToString() != "")
                    {
                        model.Sequence = int.Parse(dt.Rows[n]["Sequence"].ToString());
                    }
                    model.SQLCMD = dt.Rows[n]["SQLCMD"].ToString();
                    model.Remark = dt.Rows[n]["Remark"].ToString();


                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion


        #region  ExtensionMethod
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        #endregion  ExtensionMethod

        /// <summary>
        /// 删除当前数据字典和子集数据字典
        /// </summary>
        /// <param name="dictId">当前数据字典ID</param>
        /// <returns></returns>
        public bool DeleteChild(int dictId)
        {
            return dal.DeleteChild(dictId);
        }


        
         
        /*public List<RBS.Core.Model.SYS_DictInfo> GetModelList(string  code)
        {
            DataSet ds = dal.GetList("");
            return DataTableToList(ds.Tables[0]);
        }
*/
    }
}